package com.generic;
import java.util.*;

/**
 * @author LingLed
 * @version 1.0
 */
public class GenericExercise01 {
    public static void main(String[] args) {
        List<Employee> arrayList = new ArrayList<>();
        arrayList.add(new Employee("张三", new MyDate(1991, 2, 1), 10000));
        arrayList.add(new Employee("李四", new MyDate(1992, 2, 1), 20000));
        arrayList.add(new Employee("王五", new MyDate(1993, 2, 1), 30000));
        //使用ArrayList的sort方法排序
        arrayList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                //根据姓名排序,
                //姓名相同时使用生日排序,先生日在前
                int i = o1.getName().compareTo(o2.getName());
                if (i == 0)
                    i = o1.getBirthday().compareTo(o2.getBirthday());
                return i;

                //根据工资小到大排序
//                return (int) (o1.getSalary() - o2.getSalary());
            }
        });

        //遍历输出
        Iterator<Employee> iterator = arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

    static class Employee{
        private String name;
        private MyDate birthday;
        private double salary;

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", birthday=" + birthday +
                    ", salary=" + salary +
                    '}';
        }

        public Employee(String name, MyDate birthday, double salary) {
            this.name = name;
            this.birthday = birthday;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public MyDate getBirthday() {
            return birthday;
        }

        public void setBirthday(MyDate birthday) {
            this.birthday = birthday;
        }

        public double getSalary() {
            return salary;
        }

        public void setSalary(double salary) {
            this.salary = salary;
        }
    }

    static class MyDate implements Comparable<MyDate>{
        private int year;
        private int month;
        private int day;

        @Override
        public int compareTo(MyDate o) {
            int i;
            i = Integer.compare(this.year, o.year);
            if(i == 0)
                i = Integer.compare(this.month, o.month);
            if(i == 0)
                i = Integer.compare(this.day, o.day);
            return i;
        }

        @Override
        public String toString() {
            return "MyDate{" +
                    "year=" + year +
                    ", month=" + month +
                    ", day=" + day +
                    '}';
        }

        public MyDate(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }

        public int getYear() {
            return year;
        }

        public void setYear(int year) {
            this.year = year;
        }

        public int getMonth() {
            return month;
        }

        public void setMonth(int month) {
            this.month = month;
        }

        public int getDay() {
            return day;
        }

        public void setDay(int day) {
            this.day = day;
        }
    }
}
